package top.kaoshanji.p3ch12a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 代码清单-p3ch12a-381页a：使用反射API调用桥接方法
 * author kaoshanji
 * ^_^
 * create time 2025/2/6 9:12
 */
public class ReflectiveInvokeBridgeMethod {

    private static final Logger logger = LogManager.getLogger(ReflectiveInvokeBridgeMethod.class);

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        logger.info("...代码清单-p3ch12a-381页a：使用反射API调用桥接方法...");

        Method method = Sequence.class.getMethod("compareTo", new Class<?>[]{Object.class});
        boolean bridge = method.isBridge();
        logger.info("...compareTo是桥接方法吗？答案是：{}...", bridge);

        Sequence seq1 = new Sequence(1);
        Sequence seq2 = new Sequence(2);
        Object obj = method.invoke(seq1, seq2);
        logger.info("...调用compareTo方法：{}...", obj);

        // 会抛出 java.lang.ClassCastException 异常
        method.invoke(seq1, "Hello");
    }

}
